package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.bo.RoleListBO;
import com.junzhiit.nengji.dao.RoleDAO;
import com.junzhiit.nengji.model.Role;
import com.junzhiit.nengji.model.enums.Is;
import com.junzhiit.nengji.model.enums.IsDefault;
import com.junzhiit.nengji.model.enums.SystemUserType;

@Repository
public class RoleDAOImpl extends AbstractBaseDAO<Role> implements RoleDAO {

	@Override
	public List<Role> findAll(int pageNumber, int maxPageRow, Role role) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from `role`");
		sql.append(" where is_delete=?");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		
		if(role.getDefaultType()!=null){
			sql.append(" and (is_default= ? or ( is_default= ? and default_type = ?))");
			params.setInt(Is.NO.toIntValue());
			params.setInt(Is.YES.toIntValue());
			params.setInt(role.getDefaultType());
		}

		if (StringUtils.isNotBlank(role.getName())) {
			sql.append(" and name like ?");
			params.setString("%" + role.getName() + "%");
		}
		if (role.getPlatform() != null) {
			sql.append(" and platform = ?");
			params.setInt(role.getPlatform().intValue());
		}
		if (StringUtils.isNotBlank(role.getSystemId())) {
			if ("root".equals(role.getSystemId())) {
				sql.append(" and system_id is null");
			} else {
				sql.append(" and (system_id = ? or system_id is null)");
				params.setString(role.getSystemId());
			}
		}

		sql.append(" order by created_time desc");
		return super.queryForPagingList(sql.toString(), Role.class, params, pageNumber, maxPageRow);
	}

	@Override
	public Integer getTotalCount(Role role) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from `role`");
		sql.append(" where is_delete=?");
		
		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		if(role.getDefaultType()!=null){
			sql.append(" and (is_default= ? or ( is_default= ? and default_type = ?))");
			params.setInt(Is.NO.toIntValue());
			params.setInt(Is.YES.toIntValue());
			params.setInt(role.getDefaultType());
		}

		if (StringUtils.isNotBlank(role.getName())) {
			sql.append(" and name like ?");
			params.setString("%" + role.getName() + "%");
		}
		if (role.getPlatform() != null) {
			sql.append(" and platform = ?");
			params.setInt(role.getPlatform().intValue());
		}
		if (StringUtils.isNotBlank(role.getSystemId())) {
			if ("root".equals(role.getSystemId())) {
				sql.append(" and system_id is null");
			} else {
				sql.append(" and (system_id = ? or system_id is null)");
				params.setString(role.getSystemId());
			}
		}

		return super.queryForInt(sql.toString(), params);
	}

	@Override
	public Integer getCodeExist(String code) {
		StatementParameter params = new StatementParameter();

		StringBuffer sql = new StringBuffer();

		sql.append("select count(code) from `role` where 1 = 1");

		if (StringUtils.isNotBlank(code)) {
			sql.append(" and code=?");
			params.setString(code);
		}
		return super.queryForInt(sql.toString(), params);
	}

	@Override
	public List<Role> getAll(Role role) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select * from `role`");
		sql.append(" where is_delete=?");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		if(role.getDefaultType()!=null){
			sql.append(" and (is_default= ? or ( is_default= ? and default_type = ?))");
			params.setInt(Is.NO.toIntValue());
			params.setInt(Is.YES.toIntValue());
			params.setInt(role.getDefaultType());
		}
		
		if (role.getPlatform() != null) {
			sql.append(" and platform = ?");
			params.setInt(role.getPlatform().intValue());
		}
		if (StringUtils.isNotBlank(role.getSystemId())) {
			sql.append(" and system_id = ?");
			params.setString(role.getSystemId());
		}
		return super.queryForList(sql.toString(), Role.class, params);

	}

	@Override
	public List<RoleListBO> getAllWithFunction(Role role) {

		StringBuffer sql = new StringBuffer();
		sql.append(" select *,");
		// subquery
		sql.append(" (");
		sql.append(" select GROUP_CONCAT(f.`name`) from role_function rf");
		sql.append(" left join `function` f on rf.function_id=f.id");
		sql.append(" where rf.role_id= r.id");
		sql.append(" ) function_name");
		sql.append(" from `role` r");
		sql.append(" where is_delete=?");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		
		if(role.getDefaultType()!=null){
			sql.append(" and (is_default= ? or ( is_default= ? and default_type = ?))");
			params.setInt(Is.NO.toIntValue());
			params.setInt(Is.YES.toIntValue());
			params.setInt(role.getDefaultType());
		}
		
		if (role.getPlatform() != null) {
			sql.append(" and platform = ?");
			params.setInt(role.getPlatform().intValue());
		}
		if (StringUtils.isNotBlank(role.getSystemId())) {
			if ("root".equals(role.getSystemId())) {
				sql.append(" and system_id is null");
			} else {
				sql.append(" and (system_id = ? or system_id is null)");
				params.setString(role.getSystemId());
			}
		}
		
		return super.queryForList(sql.toString(), RoleListBO.class, params);

	}
	
	@Override
	public int getIsDefaultCount(Integer defaultType){
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(*) from role");
		sql.append(" where is_default=?");
		sql.append(" and platform=?");
		sql.append(" and is_delete=?");
		sql.append(" and default_type=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(Is.YES.toIntValue());
		param.setInt(SystemUserType.SHOP.toIntStatus());
		param.setInt(IsDelete.NO.toIntValue());
		param.setInt(defaultType);
		
		
		return super.queryForInt(sql.toString(),param);
		
	}
	
	@Override
	public Role getDefaultRole(Integer defaultType){
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from role");
		sql.append(" where is_delete=?");
		sql.append(" and is_default=?");
		sql.append(" and default_type=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setInt(IsDefault.YES.toIntStatus());
		param.setInt(defaultType);
		
		return super.query(sql.toString(), Role.class, param);
	}
}
